package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.topscore.uke.R;
import com.topscore.uke.network.request.SaveMemberAddressRequest;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.dialog.CitySelector;
import com.topscore.utillibrary.widget.CircleImageView;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CreateNewAddressActivity extends ToolBarCompatActivity implements View.OnClickListener {

    @Bind(R.id.avatar_iv)
    CircleImageView avatarIv;
    @Bind(R.id.name_edt)
    EditText nameEdt;
    @Bind(R.id.phone_edt)
    EditText phoneEdt;
    @Bind(R.id.city_tx)
    TextView cityTx;
    @Bind(R.id.select_city)
    LinearLayout selectCity;
    @Bind(R.id.address_edt)
    EditText addressEdt;
    @Bind(R.id.save_btn)
    Button saveBtn;
    @Bind(R.id.member_name_tv)
    TextView memberNameTv;
    @Bind(R.id.member_phone_tv)
    TextView memberPhoneTv;
    @Bind(R.id.arrow)
    TextView arrow;

    private CitySelector mCitySelector;

    private SaveMemberAddressRequest mSaveAddressRequest;

    private String mMemberName, mPhone, mMemberID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_new_address);
        ButterKnife.bind(this);
        setCentralTitle("添加地址");
        mMemberName = getIntent().getStringExtra("memberName");
        mPhone = getIntent().getStringExtra("phone");
        mMemberID = getIntent().getStringExtra("memberID");
        mSaveAddressRequest = new SaveMemberAddressRequest(mPhone);

        initViews();
    }

    private void initViews() {
        selectCity.setOnClickListener(this);
        saveBtn.setOnClickListener(this);
        memberNameTv.setText(mMemberName);
        memberPhoneTv.setText(mPhone);

        mCitySelector = new CitySelector(mContext).setOnCitySelectListener((province, city,district) -> {
            cityTx.setText(province.getProvinceName() + "\t" + city.getCityName()+"\t"+district.getAreaName());
            mSaveAddressRequest.setProvince(province.getProvinceName());
            mSaveAddressRequest.setProvinceId(province.getProvinceCode());
            mSaveAddressRequest.setCity(city.getCityName());
            mSaveAddressRequest.setCityId(city.getCityCode());
            mSaveAddressRequest.setDistrict(district.getAreaName());
            mSaveAddressRequest.setDistrictId(district.getAreaCode());
            setUpSaveButton();
        });

        nameEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSaveAddressRequest.setReceiverName(editable.toString());
                setUpSaveButton();
            }
        });
        phoneEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSaveAddressRequest.setReceiverContactInfo(editable.toString());
                setUpSaveButton();
            }
        });
        addressEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSaveAddressRequest.setAddress(editable.toString());
                setUpSaveButton();
            }
        });

    }

    private void setUpSaveButton() {
        if (TextUtils.isEmpty(mSaveAddressRequest.getAddress())
                || TextUtils.isEmpty(mSaveAddressRequest.getReceiverContactInfo())
                || TextUtils.isEmpty(mSaveAddressRequest.getReceiverName())
                || TextUtils.isEmpty(mSaveAddressRequest.getCityId())
                || TextUtils.isEmpty(mSaveAddressRequest.getProvinceId())) {
            saveBtn.setBackgroundColor(Color.parseColor("#dcdcdc"));
            saveBtn.setTextColor(Color.parseColor("#999999"));
        } else {
            saveBtn.setBackgroundResource(R.color.colorPrimary);
            saveBtn.setTextColor(getResources().getColor(R.color.white));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.select_city:
//                if (mCitySelector == null) {
//
//                }
                mCitySelector.show();
                break;
            case R.id.save_btn:
                if (TextUtils.isEmpty(mSaveAddressRequest.getAddress())
                        || TextUtils.isEmpty(mSaveAddressRequest.getReceiverContactInfo())
                        || TextUtils.isEmpty(mSaveAddressRequest.getReceiverName())
                        || TextUtils.isEmpty(mSaveAddressRequest.getCityId())
                        || TextUtils.isEmpty(mSaveAddressRequest.getProvinceId())) {
                    Toast.makeText(mContext, "请把地址信息填写完善", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("address_info", mSaveAddressRequest);
                    setResult(RESULT_OK, intent);
                    onBackPressed();
                }
                break;
        }
    }
}
